package com.gmss.crawler.service;

import com.alibaba.fastjson.JSON;
import com.gmss.crawler.enums.AcademicianEnum;
import com.gmss.crawler.module.test.domain.PersonInfoTopy;
import com.gmss.crawler.utils.CrawlerCommonUtils;
import org.apache.commons.lang3.StringUtils;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@Service
public class YouthService {


    public static void main(String[] args) throws IOException {
        new YouthService().crawlerData();
    }

//    public List<PersonInfo> crawlerData() throws IOException {
//        Document doc = CrawlerCommonUtils.getDocumentHtml("/htmlTest/jq-杰出青年/2015-2017年杰青.html");
//        Elements titleElements = doc.select("#t_container div .mod_body .content_title_ten .content_title_ten_text .content_title_ten_chinese");
//        Elements tbodyElements = doc.select("#t_container div .mod_body .mod_cont table tbody");
//        List<PersonInfo> list = new ArrayList<>();
//        for (int i = 3; i < tbodyElements.size(); i++) {
//            Elements trElements = tbodyElements.get(i).select("tr");
//            String electionYear = titleElements.get(i + 1).text().substring(0, 4) + "年";
//            for (int j = 1; j < trElements.size(); j++) {
//                Elements tdElements = trElements.get(j).select("td");
//                PersonInfo personInfo = new PersonInfo();
//                String name = tdElements.get(1).text();
//                if (StringUtils.contains(name, "输入文本")) {
//                    break;
//                }
//                personInfo.setName(name);
//                personInfo.setSex(tdElements.get(2).text());
//                personInfo.setAcademicDegree(tdElements.get(3).text());
//                personInfo.setPersonTitle(tdElements.get(4).text());
//                personInfo.setMajor(tdElements.get(5).text());
//                personInfo.setWorkCompany(tdElements.get(6).text());
//                personInfo.setElectionYear(electionYear);
//                personInfo.setTitleYears(4);
//                personInfo.setAcademicianTitle(12);
//                personInfo.setAcademicianTitleValue(AcademicianEnum.OUTSTANDING_YOUTH.getValue());
//                list.add(personInfo);
//            }
//        }
//        String time = "2018,2019,2020";
//        String[] split = time.split(",");
//        for (int i = 0; i < split.length; i++) {
//            Document doc1 = CrawlerCommonUtils.getDocumentHtml("/htmlTest/jq-杰出青年/" + split[i] + "-杰出青年.html");
//            Elements trEle = doc1.select("#t_container .mod_body .mod_cont table tbody tr");
//            for (int j = 1; j < trEle.size(); j++) {
//                Elements tdEle = trEle.get(j).select("td");
//                PersonInfo personInfo = new PersonInfo();
//                personInfo.setName(tdEle.get(1).text());
//                personInfo.setSex(tdEle.get(2).text());
//                personInfo.setAcademicDegree(tdEle.get(3).text());
//                personInfo.setPersonTitle(tdEle.get(4).text());
//                personInfo.setMajor(tdEle.get(5).text());
//                personInfo.setWorkCompany(tdEle.get(6).text());
//                if (!split[i].equals("2019")) {
//                    personInfo.setNationality(tdEle.get(7).text());
//                }
//                if (i == 0) {
//                    personInfo.setElectionYear("2018年");
//                } else if (i == 1) {
//                    personInfo.setElectionYear("2019年");
//                } else {
//                    personInfo.setElectionYear("2020年");
//                }
//                personInfo.setTitleYears(4);
//                personInfo.setAcademicianTitle(12);
//                personInfo.setAcademicianTitleValue(AcademicianEnum.OUTSTANDING_YOUTH.getValue());
//                list.add(personInfo);
//            }
//        }
//        System.out.println(JSON.toJSONString(list));
//        System.out.println(list.size());
//        return list;
//    }

    public List<PersonInfoTopy> crawlerData() throws IOException {
        Document doc = CrawlerCommonUtils.getDocumentHtml("/htmlTest/jq-杰出青年/2015-2017年杰青.html");
        Elements titleElements = doc.select("#t_container div .mod_body .content_title_ten .content_title_ten_text .content_title_ten_chinese");
        Elements tbodyElements = doc.select("#t_container div .mod_body .mod_cont table tbody");
        List<PersonInfoTopy> list = new ArrayList<>();
        for (int i = 3; i < tbodyElements.size(); i++) {
            Elements trElements = tbodyElements.get(i).select("tr");
            String electionYear = titleElements.get(i + 1).text().substring(0, 4) + "年";
            for (int j = 1; j < trElements.size(); j++) {
                Elements tdElements = trElements.get(j).select("td");
                PersonInfoTopy personInfo = new PersonInfoTopy();
                String name = tdElements.get(1).text();
                if (StringUtils.contains(name, "输入文本")) {
                    break;
                }
                personInfo.setName(name);
                personInfo.setSex(tdElements.get(2).text());
                personInfo.setAcademicDegree(tdElements.get(3).text());
                personInfo.setPersonTitle(tdElements.get(4).text());
                personInfo.setMajor(tdElements.get(5).text().replaceAll("（", "(").replaceAll("）", ")"));
                personInfo.setWorkCompany(tdElements.get(6).text().replaceAll("（", "(").replaceAll("）", ")"));
                personInfo.setElectionYear(electionYear);
                personInfo.setTitleYears(4);
                personInfo.setAcademicianTitle(12);
                personInfo.setAcademicianTitleValue(AcademicianEnum.OUTSTANDING_YOUTH.getValue());
                if (personInfo.getName().length() > 1)
                    list.add(personInfo);
            }
        }
        String time = "2018,2019,2020";
        String[] split = time.split(",");
        for (int i = 0; i < split.length; i++) {
            Document doc1 = CrawlerCommonUtils.getDocumentHtml("/htmlTest/jq-杰出青年/" + split[i] + "-杰出青年.html");
            Elements trEle = doc1.select("#t_container .mod_body .mod_cont table tbody tr");
            for (int j = 1; j < trEle.size(); j++) {
                Elements tdEle = trEle.get(j).select("td");
                PersonInfoTopy personInfo = new PersonInfoTopy();
                personInfo.setName(tdEle.get(1).text());
                personInfo.setSex(tdEle.get(2).text());
                personInfo.setAcademicDegree(tdEle.get(3).text());
                personInfo.setPersonTitle(tdEle.get(4).text());
                personInfo.setMajor(tdEle.get(5).text().replaceAll("（", "(").replaceAll("）", ")"));
                personInfo.setWorkCompany(tdEle.get(6).text().replaceAll("（", "(").replaceAll("）", ")"));
                if (!split[i].equals("2019")) {
                    personInfo.setNationality(tdEle.get(7).text());
                }
                if (i == 0) {
                    personInfo.setElectionYear("2018年");
                } else if (i == 1) {
                    personInfo.setElectionYear("2019年");
                } else {
                    personInfo.setElectionYear("2020年");
                }
                personInfo.setTitleYears(4);
                personInfo.setAcademicianTitle(12);
                personInfo.setAcademicianTitleValue(AcademicianEnum.OUTSTANDING_YOUTH.getValue());
                list.add(personInfo);
            }
        }
        System.out.println(JSON.toJSONString(list));
        for (PersonInfoTopy personInfoTopy : list) {
            if (personInfoTopy.getName().length() == 1) {
                System.out.println(JSON.toJSONString(personInfoTopy));
            }
        }
        System.out.println(list.size());
        return list;
    }
}
